package com.huizhonglm.diap.ui.user;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.R;
import com.huizhonglm.diap.api.model.user.RcmmerInfo;
import com.huizhonglm.diap.core.image.Thumb;
import com.huizhonglm.diap.core.storage.model.ReferrerInfo;
import com.huizhonglm.diap.core.utils.LogUtil;
import com.huizhonglm.diap.core.utils.ToastUtil;
import com.huizhonglm.diap.core.utils.UserUtil;
import com.huizhonglm.diap.core.zxing.CaptureActivity;
import com.huizhonglm.diap.presenters.user.RcmCashBackPresenter;
import com.huizhonglm.diap.presenters.wallet.RcmUserPresenter;
import com.huizhonglm.diap.ui.BaseActivity;
import com.huizhonglm.diap.ui.user.iView.RcmCashBackIView;
import com.huizhonglm.diap.ui.wallet.iView.RcmUserIView;
import com.huizhonglm.diap.widget.LoadView;
import com.huizhonglm.diap.widget.RoundImageView;

import java.util.List;

/**
 *
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-03-22 13:42
 */
public class RcmCashBackActivity extends BaseActivity implements RcmCashBackIView,RcmUserIView ,View.OnClickListener,
        LoadView.OnLoadAgainListener {
    private final int REQ_CODE_CAPTURE_QR_CODE = 1;

    private GridView mGridView;
    private LoadView loadView;
    private TextView cntTxt;
    private TextView saoTxt;
    private View refeLayout,referrer_layout_add;
    private RoundImageView refeIconImg;
    private TextView refeNameTxt;
    private EditText editTextRecommendPhoneNo;
    private TextView textViewRecommendBind;

    private List<RcmmerInfo> rcmerData;
    private RcmUserPresenter mRcmUserPresenter;
    private RcmCashBackPresenter mPresenter;

    private String userID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rcm_cash_back);
        setBackEvent(this, null);
        userID = UserUtil.getUserId();
        mPresenter = new RcmCashBackPresenter(this);
        mRcmUserPresenter = new RcmUserPresenter(this);
        assignViews();
        showProgressBar();
        mPresenter.getReferrer(userID);
    }

    @Override
    protected void assignViews() {
        refeIconImg = (RoundImageView) findViewById(R.id.referrer_icon_rimg);
        refeNameTxt = (TextView) findViewById(R.id.referrer_name_txt);
        saoTxt = (TextView) findViewById(R.id.saoyisao_txt);
        editTextRecommendPhoneNo = (EditText) findViewById(R.id.editTextRecommendPhoneNo);
        textViewRecommendBind = (TextView) findViewById(R.id.textViewRecommendBind);

        refeLayout = findViewById(R.id.referrer_layout);
        referrer_layout_add = findViewById(R.id.referrer_layout_add);
        cntTxt = (TextView) findViewById(R.id.rcmmer_cnt_txt);
        mGridView = (GridView) findViewById(R.id.rcmmer_grid);
        loadView = (LoadView) findViewById(R.id.load_view);
        loadView.setDataView(mGridView, this);

        saoTxt.setOnClickListener(this);
        textViewRecommendBind.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.saoyisao_txt:
                Intent intent = new Intent();
                intent.setClass(this, CaptureActivity.class);
                startActivityForResult(intent, REQ_CODE_CAPTURE_QR_CODE);
                break;
            case R.id.textViewRecommendBind:
                final String phoneNo = editTextRecommendPhoneNo.getText().toString().trim();
                if (EmptyUtil.isEmpty(phoneNo)) {
                    ToastUtil.makeTextShort("请输入推荐人手机号");
                    return;
                }
                if (phoneNo.length()!=11) {
                    ToastUtil.makeTextShort("推荐人手机号不是有效的11位手机号");
                    return;
                }
                mRcmUserPresenter.loadData(userID,phoneNo);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQ_CODE_CAPTURE_QR_CODE:
                if (resultCode == RESULT_OK) {
                    String result = data.getStringExtra("result");
                    LogUtil.print("referrerId_capture", "--- result=" + result);
                    if (!EmptyUtil.isEmpty(result)) {
                        Uri uri = Uri.parse(result);
                        String referrerId = uri.getQueryParameter("referrerId");
                        LogUtil.print("referrerId_capture", "--- referrerId - " + referrerId);
                        if (!referrerId.equals(userID)) {// 自己不能扫自己
                            boolean isRcmmer = false;
                            if (!EmptyUtil.isEmpty(rcmerData)) {
                                for (RcmmerInfo info : rcmerData) {
                                    if (referrerId.equals(info.getRecpeoId())) {
                                        isRcmmer = true;
                                        break;
                                    }
                                }
                            }
                            if (!isRcmmer) {
                                showProgressBar();
                                mPresenter.addReferrer(userID, referrerId);
                            } else {
                                ToastUtil.makeTextShort("该用户已被您推荐了");
                            }
                        } else {
                            ToastUtil.makeTextShort("自己不能成为自己的推荐者");
                        }

                    }
                }
                break;
        }
    }

    @Override
    public void onLoadAgain() {
        mPresenter.loadData(userID);
    }

    @Override
    public void onGetReferrerInfo(ReferrerInfo info) {
        hideProgressBar();
        if (info != null && !EmptyUtil.isEmpty(info.getReferrerName())) {
            onAddReferrerSuccess(info);
        } else {
            onAddReferrerFailure();
        }
        onLoadAgain();
    }

    @Override
    public void onAddReferrerSuccess(ReferrerInfo info) {
        hideProgressBar();
        saoTxt.setVisibility(View.GONE);
        refeLayout.setVisibility(View.VISIBLE);
        referrer_layout_add.setVisibility(View.GONE);
        Thumb.getInstance().display(refeIconImg, info.getReferrerIcon(), R.drawable.def_icon);
        refeNameTxt.setText(info.getReferrerName());
    }

    @Override
    public void onAddReferrerFailure() {
        hideProgressBar();
        saoTxt.setVisibility(View.VISIBLE);
        refeLayout.setVisibility(View.GONE);
        referrer_layout_add.setVisibility(View.VISIBLE);
    }

    @Override
    public void onLoadDataSuccess(List<RcmmerInfo> data) {
        rcmerData = data;
        if (!EmptyUtil.isEmpty(data)) {
            cntTxt.setText(data.size() + "人");
            mGridView.setAdapter(new RcmmerListAdapter(getLayoutInflater(), data));
            loadView.showDataView();
        } else {
            cntTxt.setText("0人");
            loadView.showEmptyView("您还没有推荐过");
        }
        cntTxt.setVisibility(View.VISIBLE);
    }

    @Override
    public void onLoadDataFailure() {
        loadView.showErrorView();
        cntTxt.setVisibility(View.GONE);
    }

    @Override
    public void onBindUserFailure() {
        ToastUtil.makeTextShort("绑定失败");
    }

    @Override
    public void onBindUserSuccess() {
        ToastUtil.makeTextShort("绑定成功");
        editTextRecommendPhoneNo.setText("");
        mPresenter.getReferrer(userID);
    }
}
